package org.kms.Controllers;

import org.apache.ibatis.annotations.Param;
import org.kms.dao.*;
import org.kms.pojo.*;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/Te")
@CrossOrigin(value = "*")
public class teController {
    @Autowired
    private skdetailsDao skdetailsDao;

    @Autowired
    private selectedchargesDao selectedchargesDao;

    @Autowired
    private childDao childDao;

    @Autowired
    private manageDao manageDao;

    @Autowired
    private fxinfoDao fxinfoDao;

    public static int getMonthSpace(String date1, String date2) throws Exception{

        int result = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(sdf.parse(date1));
        c2.setTime(sdf.parse(date2));

        result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);

        return result == 0 ? 1 : Math.abs(result)+1;

    }

    @RequestMapping(value = "/index")
    @ResponseBody
    public ResponseResult<List<Skdetails>> getall(String deStartyears, String deEndyears, Integer cmId)throws Exception {
        List<Skdetails>  a = null;

        if (deStartyears != "" && deEndyears != "" && cmId != null && cmId != -1) {
           a= skdetailsDao.getbytime(deStartyears, deEndyears, cmId);

            Integer[] inta = new Integer[a.size()];
            Manage manage = manageDao.getbyCMId(cmId);
            for (int l = 0; l < inta.length; l++) {
                inta[l] = a.get(l).getPid();
            }
            for (int j = 0; j < inta.length; j++) {
                Child childList1 = childDao.getbychilds(inta[j]);

                a.get(j).setChild(childList1);
                a.get(j).getChild().setManage(manage);
            }

           for (int q = 0; q < inta.length; q++) {
                List<Skdetails> skdetailsList = skdetailsDao.getbyPid(deStartyears, deEndyears, inta[q]);
                List<Fxinfo> fxinfos = new ArrayList<Fxinfo>();
                for (Skdetails t : skdetailsList
                ) {
                    for (Fxinfo p : t.getFxinfos()
                    ) {
                        fxinfos.add(p);
                    }
                    a.get(q).getChild().setFxinfos(fxinfos);
                }

            }

        }

        return new ResponseResult<>(a);
    }

    @RequestMapping(value = "/fxName")
    @ResponseBody
    public List<String> ListName(String deStartyears, String deEndyears, Integer cmId)throws Exception{
        List<Selectedcharges> selectedchargesList =selectedchargesDao.getbytime(deStartyears,deEndyears,cmId);
        List<Selectedcharges> getbyclid = selectedchargesDao.getbyclid(deStartyears,deEndyears,cmId);
        double qian = 0;
        skdetailsDao.delegetbypid(deStartyears, deEndyears);
        Map<String, Object> map = new HashMap<>();
        List<Fxinfo> fx =fxinfoDao.getAll();
        int monthCha = teController.getMonthSpace(deStartyears, deEndyears);
        for (int i = 0; i < selectedchargesList.size(); i++) {
            List<Selectedcharges> fxinfos  = selectedchargesDao.getbyPid2(deStartyears,deEndyears,selectedchargesList.get(i).getPid());
                qian = 0;
          for (Selectedcharges s:fxinfos
                 ) {
              Fxinfo fxinfo = fxinfoDao.get(s.getcIid());
              Skdetails skdetails = new Skdetails();
              skdetails.setPid(selectedchargesList.get(i).getPid());
              skdetails.setcIid(s.getcIid());
              skdetails.setDeStartyears(deStartyears);
              skdetails.setDeEndyears(deEndyears);
              skdetails.setTotalSum(fxinfo.getPrice()*monthCha);
              qian=qian+fxinfo.getPrice();
              skdetailsDao.add(skdetails);
            }
            int a = skdetailsDao.getbytotle(deStartyears,deEndyears,selectedchargesList.get(i).getPid());
          Skdetails skdetails = new Skdetails();
          skdetails.setTotalSum(a);
          skdetails.setPid(selectedchargesList.get(i).getPid());
          skdetails.setDeStartyears(deStartyears);
          skdetails.setDeEndyears(deEndyears);
          skdetailsDao.update(skdetails);
        }
        return skdetailsDao.fxNames(deStartyears, deEndyears, cmId);
    }

    @RequestMapping(value = "/pidtotle")
    @ResponseBody
    public Integer gettotle(String deStartyears, String deEndyears, Integer pid) throws Exception{
        return skdetailsDao.getbytotle(deStartyears,deEndyears,pid);
    }

/*    public static void main(String[] args) {
        String a = "2018-01-01";
        String b ="2018-12-31";
        try {
            int monthCha = teController.getMonthSpace(a,b);
            System.out.println(monthCha);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}

